package com.train.springboot.springboottrain.config;

import com.train.springboot.springboottrain.filter.Myfilter2;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;

@Configuration  //定义此类为配置类
public class FilterConfig {

    @Bean
    public FilterRegistrationBean myFilterRegistrationBean(){

        //创建字符编码过滤器
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        //设置强制使用指定字符编码
        characterEncodingFilter.setForceEncoding(true);
        //设置指定字符编码
        characterEncodingFilter.setEncoding("utf-8");


        //注册过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new Myfilter2());

        //设置字符编码过滤器
        filterRegistrationBean.setFilter(characterEncodingFilter);
        //设置字符编码过滤器路径
        filterRegistrationBean.addUrlPatterns("/*");
//设置完之后，需要在springboot配置文件中，关闭 springboot的http字符编码支持
        //添加过滤器路径,这里注意要写单个   *
        filterRegistrationBean.addUrlPatterns("/user1/*");
        return filterRegistrationBean;




    }
}
